package com.le.controllers;

import com.le.bean.Board;
import com.le.biz.BoardBiz;
import com.sun.corba.se.spi.ior.ObjectKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("board") // http://localhost:port/board
@Slf4j
@Api(tags = "板块")
public class BoardController {
    @Autowired
    private BoardBiz boardBiz;

    @GetMapping("findBoardById/{boardid}") // {boardid}路径参数
    @ApiOperation(value = "根据板块号查询操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "boardid", value = "板块号", required = true)
    })
    public Map<String, Object> findBoardById(@PathVariable Integer boardid){
        Map<String, Object> map = new HashMap<>();
        Board board = null;

        try {
            board = this.boardBiz.findBoardById(boardid);
        } catch (Exception e){

            e.printStackTrace();
            map.put("code", 0);
            map.put("msg", e.getCause());
            return map;
        }

        map.put("code", 1);
        map.put("msg", board);
        return map;
    }

    @GetMapping("findAllBoard")
    @ApiOperation(value = "查询所有板块")
    public Map<String, Object> findAll(){
        Map<String, Object> map = new HashMap<>();
        //  父板块号   子版块列表
        Map<Integer, List<Board>> mapList = null;
        try {
            mapList = this.boardBiz.findAllBoard();
        } catch (Exception e){
            e.printStackTrace();
            map.put("code", 0);
            map.put("msg", e.getCause());
            return map;
        }
        map.put("code", 1);
        map.put("data", mapList);
        return map;
    }
}